package com.bank.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bank.entity.Customer_info;

@Service
public class CustomerDao {
	
	Logger logger = Logger.getLogger(Customer_info.class);
	
	
	@Autowired
	private SessionFactory sessionFactory;

	
	public void save(Customer_info customer_info){
		Session session = sessionFactory.getCurrentSession();
		session.save(customer_info);
	}
	@SuppressWarnings("unchecked") 
	public Customer_info getCustomer(String customer_number){
		
		String sql = " from Customer_info  where customer_number='"+customer_number+"'";
		Query query =  sessionFactory.getCurrentSession().createQuery(sql);
		List<Customer_info> list =  query.list();
		Customer_info customer = null;
		logger.debug("list.size()"+list.size());
		if(list.size()==0)
			return customer;
		logger.debug(list.size());
		for( Customer_info customer_info:list){
			customer = customer_info;
		}
		return customer;

	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void updateCustomer(Customer_info customer_info){
		Session session = sessionFactory.getCurrentSession();
		session.update(customer_info);
		session.flush();
	}
}
